<script setup>
import {useRoute,useRouter} from 'vue-router'
import {onMounted,ref,reactive ,computed } from 'vue'
import {findById} from '@/api/careRecord'
import {findById as findByelderId} from '@/api/elderInfo'
import {findById as findByCaregiverId} from '@/api/caregiver'
import {findById as findByItemId} from '@/api/careContent'
import dayjs from 'dayjs'

const formatTime = (time) => {
  return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
}

const statusInfo = computed(() => {
  return statusMap[careRecord.value.status] || { text: '未知状态', type: '' };
});
const route =useRoute()
const router =useRouter()
// 添加健康状态标签类型映射
const getHealthStatusTag = (status) => {
  const map = {
    1: 'success',
    2: '',
    3: 'warning',
    4: 'danger'
  };
  return map[status] || '';
};

onMounted(async () => {
    const id = route.query.id;
    try {
        // 获取护理记录信息
        const careRes = await findById(id);
        careRecord.value = careRes.data;
        
        // 获取老人信息
        const elderRes = await findByelderId(careRecord.value.elderlyId);
        elderInfo.value = elderRes.data;
        
        // 获取护理员信息
        const caregiverRes = await findByCaregiverId(careRecord.value.caregiverId);
        caregiverInfo.value = caregiverRes.data;
        // 获取护理员信息
        const careContentRes = await findByItemId(careRecord.value.itemId);
        careContentInfo.value = careContentRes.data;
        
        
    } catch (error) {
        console.error("请求出错:", error);
    }
});

const careRecord = ref({})
const elderInfo = ref({})
const caregiverInfo = ref({})
const careContentInfo= ref({})

const handleBack=()=>{
    router.back()
}

// 护理类型映射
const careTypeMap = {
  1: ' 基础护理',
  2: '康复护理',
  3: '心理护理',
  4: '医疗护理',
  5: '其他护理'
}


// 护理记录状态映射
const statusMap = {
  0: { text: '待执行', type: 'info' },
  1: { text: '已完成', type: 'primary' },
  2: { text: '异常', type: 'success' },
  3: { text: '已取消', type: 'danger' }
}
// 性别映射
const genderMap = {
  0: '女',
  1: '男'
}
// 健康状态映射
const healthStatusMap = {
  1: '良好',
  2: '一般',
  3: '较差',
  4: '病重',

}
</script>
<template>
  <div class="care-detail-container">
    <!-- 头部卡片 -->
    <el-card class="header-card" shadow="hover">
      <div class="header-content">
        <h1 class="page-title">
          <el-icon class="title-icon"><Document /></el-icon>
          护理记录详细信息
        </h1>
        <div class="record-id">记录编号: {{ careRecord.careId || '无' }}</div>
      </div>
    </el-card>

    <!-- 内容区域 -->
    <div class="content-container">
      <el-card class="info-card" shadow="never">
        <!-- 基本信息 -->
        <div class="info-section">
          <div class="section-header">
            <el-icon><InfoFilled /></el-icon>
            <h2>护理信息</h2>
          </div>
          <el-divider class="custom-divider" />
          <div class="info-grid">
            <div class="info-item">
              <span class="info-label">护理类型：</span>
              <el-tag class="care-type-tag" effect="light">
                {{ careTypeMap[careRecord.careType] || '未知类型' }}
              </el-tag>
            </div>
            <div class="info-item">
              <span class="info-label">护理时间：</span>
              <span class="info-value">{{ formatTime(careRecord.careTime) }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">护理内容：</span>
              <span class="info-value">{{ careRecord.careContent || '无' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">护理项目：</span>
              <span class="info-value">{{ careContentInfo.name || '无' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">状态：</span>
              <el-tag :type="statusInfo.type" class="status-tag">
                {{ statusInfo.text }}
              </el-tag>
            </div>
          </div>
        </div>

        <!-- 老人信息 -->
        <div class="info-section">
          <div class="section-header">
            <el-icon><UserFilled /></el-icon>
            <h2>老人信息</h2>
          </div>
          <el-divider class="custom-divider" />
          <div class="info-grid">
            <div class="info-item">
              <span class="info-label">姓名：</span>
              <span class="info-value">{{ elderInfo.name || '无' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">性别：</span>
              <span class="info-value">{{ genderMap[elderInfo.gender] || '未知' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">年龄：</span>
              <span class="info-value">{{ elderInfo.age || '无' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">健康状态：</span>
              <el-tag :type="getHealthStatusTag(elderInfo.healthStatus)" class="health-tag">
                {{ healthStatusMap[elderInfo.healthStatus] || '未知' }}
              </el-tag>
            </div>
          </div>
        </div>

        <!-- 护理员信息 -->
        <div class="info-section">
          <div class="section-header">
            <el-icon><Avatar /></el-icon>
            <h2>护理员信息</h2>
          </div>
          <el-divider class="custom-divider" />
          <div class="info-grid">
            <div class="info-item">
              <span class="info-label">姓名：</span>
              <span class="info-value">{{ caregiverInfo.employee?.name || '无' }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">护理评分：</span>
              <el-rate
                v-model="careRecord.evaluation"
                disabled
                :max="5"
                :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
                class="rating-stars"
              />
            </div>
            <div class="info-item full-width">
              <span class="info-label">备注：</span>
              <span class="info-value">{{ careRecord.remark || '无' }}</span>
            </div>
          </div>
        </div>
      </el-card>

      <!-- 返回按钮 -->
      <div class="action-buttons">
        <el-button type="primary" @click="handleBack" class="back-button">
          <el-icon class="button-icon"><Back /></el-icon>
          返回
        </el-button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.care-detail-container {
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
  min-height: 100vh;
}

/* 头部卡片 */
.header-card {
  margin-bottom: 20px;
  border-radius: 16px;
  background: linear-gradient(120deg, #4a6bdf 0%, #3a56c4 100%);
  border: none;
  overflow: hidden;
  box-shadow: 0 6px 18px rgba(65, 105, 225, 0.25);
}

.header-content {
  display: flex;
  align-items: center;
  padding: 20px 30px;
  color: white;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  margin: 0 15px 0 0;
  color: white;
  display: flex;
  align-items: center;
}

.title-icon {
  font-size: 28px;
  color: #ffd04b;
  margin-right: 12px;
}

.record-id {
  margin-left: auto;
  font-size: 16px;
  opacity: 0.9;
}

/* 内容容器 */
.content-container {
  display: flex;
  flex-direction: column;
}

.info-card {
  border-radius: 16px;
  border: none;
  background: white;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  margin-bottom: 20px;
}

/* 信息区块 */
.info-section {
  padding: 25px 30px;
  border-bottom: 1px solid #f0f4f8;
}

.info-section:last-child {
  border-bottom: none;
}

.section-header {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.section-header h2 {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  margin-left: 10px;
}

.section-header .el-icon {
  font-size: 22px;
  color: #4a6bdf;
}

.custom-divider {
  margin: 10px 0 20px;
  background-color: #f0f4f8;
}

/* 信息网格布局 */
.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px 30px;
}

.info-item {
  display: flex;
  align-items: center;
  padding: 8px 0;
}

.full-width {
  grid-column: 1 / -1;
}

.info-label {
  font-weight: 500;
  color: #5a6d88;
  min-width: 100px;
  text-align: right;
  margin-right: 15px;
}

.info-value {
  font-weight: 500;
  color: #34495e;
  flex: 1;
  line-height: 1.6;
}

/* 标签样式 */
.care-type-tag {
  font-weight: 500;
  background-color: #f0f7ff;
  color: #4a6bdf;
  border: none;
}

.status-tag {
  font-weight: 500;
  padding: 6px 12px;
  border-radius: 16px;
}

.health-tag {
  font-weight: 500;
  padding: 6px 12px;
  border-radius: 16px;
}

/* 评分星星 */
.rating-stars {
  display: inline-flex;
}

/* 操作按钮 */
.action-buttons {
  text-align: center;
  margin-top: 20px;
}

.back-button {
  width: 140px;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(120deg, #4a6bdf 0%, #3a56c4 100%);
  border: none;
  box-shadow: 0 4px 12px rgba(65, 105, 225, 0.3);
  transition: all 0.3s ease;
}

.back-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 15px rgba(65, 105, 225, 0.4);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .record-id {
    margin: 10px 0 0;
    align-self: flex-start;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .info-label {
    min-width: 90px;
    text-align: left;
  }
}
</style>